import 'package:flutter_learn_vi/src/bean/bean_user.dart';
import 'package:flutter_learn_vi/src/common/user_helper.dart';
import 'package:flutter_learn_vi/src/net/dio_utils.dart';
import 'package:get/get.dart';

import '../../../../bean/bean_score_integral.dart';
import '../../../../utils/log_util.dart';

class ScroeIntegralRecoderController extends GetxController {
  int totalCount = 0;
  int totalPage = 0;
  int currPage = 0;
  int pageSize = 100;
  int userId;

  String startDate;

  bool isLoading = false;

  ScroeIntegralRecoderController(this.userId, this.startDate);

  List<ScoreIntegralWaterBean> waterList = [];

  Future<bool> requestWaterList({bool isRefresh = false, int id}) async {
    if (id != null) {
      userId = id;
    }
    isLoading = true;
    if (isRefresh) {
      update(["IntegralRecoderWater"]);
    }

    UserBean userBean = UserHelper.getInstance.userBean;
    Map<String, dynamic> map2 = {};
    map2["mineId"] = userBean.mineId;
    map2["userId"] = userId;
    map2["recoderType"] = -1;
    map2["flag"] = 0;
    map2["pageIndex"] = currPage;
    map2["pageSize"] = pageSize;
    map2["startDate"] = startDate + "-01 00:00:00";

    LogUtils.e("查看积分记录 网络请求 $userId");
    ResponseInfo responseInfo = await DioUtils.instance
        .getRequest(url: HttpHelper.integralRecoderUrl, queryParameters: map2);
    waterList = [];
    if (responseInfo.success) {
      //解析数据
      dynamic data = responseInfo.data;
      dynamic list = data;
      if (list != null) {
        for (dynamic item in list) {
          ScoreIntegralWaterBean bean = ScoreIntegralWaterBean.fromMap(item);
          waterList.add(bean);
        }
      } else {
        waterList = [];
      }
    } else {
      waterList = [];
    }
    isLoading = false;
    update(["IntegralRecoderWater"]);
    return true;
  }

  Future<bool> onRefresh() async {
    await requestWaterList();
    return true;
  }

  @override
  void onReady() {
    super.onReady();
  }
}
